//
//  FNumField.swift
//  Aiwei
//
//  Created by zhu xietong on 2017/11/15.
//  Copyright © 2017年 zhu xietong. All rights reserved.
//

import Foundation
import Eelay

open class NumField: FieldView {
    public let valueL = UILabel()
    public let nameL = UILabel()
    public let iconV = UIImageView()
    public let bt = UIButton()
    open override func addLayoutRules() {
        self.eelay = [
            [nameL,[ee.L.Y],70],
            [iconV,[ee.R.Y],"10",10],
            [valueL,[ee.Y],[nameL,ee.R,ee.L,4],[iconV,ee.L,ee.R,-16]],
            [bt,[ee.T.L.B.R]],
        ]
        
        nameL.ui.font13.cl_222.left.text("\("数量".loc):")
//        iconV.image = UIImage(named:"sc_xjiantou")
        _ = valueL.ui.left.cl_222.font14.text("1")
    }
    open override func didLayoutRules() {
        self.isUserInteractionEnabled = true
        bt.addTarget(self, action: #selector(selectAction), for: .touchUpInside)
    }
    
    override open func get_string_value() -> String {
        return self.mo.model[FK.value,"1"]
    }
    
    @objc open func selectAction()
    {
        if mo.model[FK.editble,"YES"] == "NO"
        {
            return
        }
        NotificationCenter.default.post(name: NSNotification.Name(rawValue: FK.SaveJoFormNotify), object: nil)
        weak var wself = self
        let ctr = NumPopSelector()
        ctr.value = mo.model[int:FK.value,1]
        ctr.selectValue = {
            wself?.mo.model[FK.value] = "\($0)"
            wself?.mo_model_reload()
        }
        self.controller?.present(ctr, animated: false, completion: {
            
        })
    }
    
    open override func mo_model_reload() {
        nameL.text = mo.model["title",""]
        mo.model[FK.value] = mo.model[FK.value,"1"]
        self.valueL.text = mo.model[FK.value,"1"]
    }
    
    override open var intrinsicContentSize: CGSize{
        get{
            return [120,42]
        }
    }
}

open class NumBorderField: FieldView {
    
    public var limit = NumLimit()

    public let valueL = UILabel()
    public let nameL = UILabel()
    public let iconV = UIImageView()
    
    open override func addLayoutRules() {
        let bt = UIButton()
        self.eelay = [
            [nameL,[ee.L.Y,[10,0]],40],
            [iconV,[ee.R.Y,[-10,0]],"10",10],
            [valueL,[ee.Y],[nameL,ee.R,ee.L,4],[iconV,ee.L,ee.R,-16]],
            [bt,[ee.T.L.B.R]],
        ]
        
        nameL.ui.font11.cl_222.left.text("\("数量".loc):")
        iconV.image = UIImage(named:"sc_xjiantou")
        _ = valueL.ui.center.cl_222.font12.text("1")
        self.isUserInteractionEnabled = true
        bt.addTarget(self, action: #selector(selectAction), for: .touchUpInside)
        self.bsui.set(radius: 0, width: 1, color: UIColor(shex:"#ddd"))
    }
    
    override open func get_string_value() -> String {
        return self.mo.model[FK.value,"1"]
    }
    
    @objc open func selectAction()
    {
        if mo.model[FK.editble,"YES"] == "NO"
        {
            return
        }
        
        if limit.max < limit.min{
            limit.maxMask.alert()
            return
        }

        NotificationCenter.default.post(name: NSNotification.Name(rawValue: FK.SaveJoFormNotify), object: nil)
        weak var wself = self
        let ctr = NumPopSelector()
        ctr.limit = self.limit
        ctr.value = mo.model[int:FK.value,1]
        ctr.selectValue = {
            wself?.mo.model[FK.value] = "\($0)"
            wself?.mo_model_reload()
        }
        self.controller?.present(ctr, animated: false, completion: {
            
        })
    }
    
    open override func mo_model_reload() {
        mo.model[FK.value] = mo.model[FK.value,"1"]
        self.valueL.text = mo.model[FK.value,"1"]
    }
    
//    override var intrinsicContentSize: CGSize{
//        get{
//            return [120,36]
//        }
//    }
}
